import {
  addActivity,
  getActivities,
  updateActivity,
  addInform,
  delActivity,
  delInform,
  updateInform,
  getNeedSigninActivities,
  getActivitySigninList
} from "@/api/activity";

const state = {
  activities: [],
  total: 0,
  signinList:[]
}
const mutations = {
  SET_ACTIVITIES(state, data) {
    state.activities = data;
  },
  SET_TOTAL(state, data) {
    state.total = data;
  },
  SET_INFORMS(state, data) {
    let index = state.activities.findIndex(item=>item.id == data.activity.id);
    state.activities[index].informs = data.informs;
  },
  RESET_STATE(state,data){
    state = {
      activities: [],
      total: 0,
      informs: [],
    }
  },
  SET_SIGNINLIST(state,data){
    state.signinList = data;
  }
}

const actions = {
  async addActivity({commit}, data) {
    let result = await addActivity(data);
    if (result.code === 200) {
      return 'OK'
    }
  },
  async delActivity({commit}, ids) {
    let result = await delActivity(ids);
    if (result.code === 200) {
      return 'OK'
    }
  },
  async getActivities({commit}, data) { // data中有needInform时，返回携带informs的activity
    let result = await getActivities(data);
    if (result.code === 200) {
      commit('SET_ACTIVITIES', result.data.activities);
      commit('SET_TOTAL', result.data.total)
    }
  },
  async updateActivity({commit}, data) {
    let result = await updateActivity(data);
    if (result.code === 200) {
      return 'OK'
    }
  },
  async addInform(_, data) {
    let result = await addInform(data);
    if (result.code === 200) {
      return 'OK'
    }
  },
  async updateInform({commit},data){
    let result = await updateInform(data);
    if (result.code === 200){
      return 'ok';
    }
  },
  async deleteInform({commit},id){
    let result = await delInform(id);
    if (result.code === 200){
      return 'ok';
    }
  },
  // 获取需要签到的活动信息
  async getNeedSigninActivities({commit},data){
    let result = await getNeedSigninActivities(data);
    if (result.code === 200){
      commit('SET_ACTIVITIES',result.activities);
      commit('SET_TOTAL',result.total)
    }
    return result.code;
  },
  // 获取该活动已签到人的信息
  async getActivitySigninList({commit},activity_id) {
    let result = await getActivitySigninList(activity_id);
    if (result.code === 200){
      commit('SET_SIGNINLIST',result.signinList);
    }
    return result.code
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

